﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SpreadsheetGear;
using WorkbookDesigner.Addons.SpreadsheetEditor.Model;
using WorkbookDesigner.Addons.SpreadsheetEditor.UI;

namespace WorkbookDesigner.Addons.SpreadsheetEditor.Presenter
{
    public class MissionPresenter
    {
        public MissionPresenter(IWorkbookSet wbs, IMissionView view, IBookView bView)
        {
            Wbs = wbs;
            MissionView = view;
            BookView = bView;
        }

        public void Init(IMission mission)
        {
            Mission = mission;
        }
        public IMission Mission { get; private set; }
        private IMissionView MissionView { get; }
        private IBookView BookView { get; }
        private IWorkbookSet Wbs { get; }

        public List<ActionModel> AllAction { get; } = new List<ActionModel>();

        private IList<ActionModel> DeleteAcList => AllAction.Where(x => x.OperationType == OpType.Delete).ToList();
        private IList<ActionModel> AddAcList => AllAction.Where(x => x.OperationType == OpType.New).ToList();
        private IList<ActionModel> EditAcList => AllAction.Where(x => x.OperationType == OpType.Edit).ToList();
        private IList<ActionModel> GetWorkingList()
        {
            switch (MissionView.WorkType)
            {
                case OpType.New:
                    return AddAcList;
                case OpType.Edit:
                    return EditAcList;
                case OpType.Delete:
                    return DeleteAcList;
                default:
                    return EditAcList;
            }
        }

        public void OnListView_RetrieveVirtualItem(RetrieveVirtualItemEventArgs e)
        {
            Wbs.GetLock();
            var model = GetWorkingList()[e.ItemIndex];
            PromptModel pm = model.Template;
            string[] str =
            {
                pm.Name,
                pm.Value,
                pm.ControlType,
                pm.HelpMessage,
                pm.VerifyCode,
                pm.ComboString,
                pm.Color,
                pm.Picture,
                pm.VisibleRange,
                pm.HideInReport,
                pm.TabIndex,
                pm.CalculatorIndex,
            };
            ListViewItem.ListViewSubItem[] sa = str.Select(s => new ListViewItem.ListViewSubItem
            {
                Text = s ?? "",
                //ForeColor = s != null && s.StartsWith("=") ? System.Drawing.Color.Red : System.Drawing.Color.Black,
            }).ToArray();
            ListViewItem item = new ListViewItem(sa, 0)
            {
                Tag = model
            };
            Wbs.ReleaseLock();
            e.Item = item;
        }

        public void OnActionSelectedIndexChanged()
        {
            Wbs.GetLock();
            RefreshActionListView();
            Wbs.ReleaseLock();
            MissionView.ActionViews.Refresh();

        }

        public void NewAction_Add(IRange rangeSelection)
        {
            Wbs.GetLock();
            if (rangeSelection == null)
            {

                return;
            }
            IRange selectedRows = rangeSelection.Rows;
            foreach (IRange row in selectedRows)//ps 每行下面都插入 还是插入到最后一行下方？
            {
                PromptModel pm = new PromptModel(row.EntireRow);
                PromptModel newp = new PromptModel() { VisiblePrompt = pm.VisiblePrompt };
                using (PromptSetForm pf = new PromptSetForm(newp) { Text = $"请输入新变量的各项数值" })
                {
                    if (pf.ShowDialog() != DialogResult.OK)
                        continue;
                    pf.SyncPrompt(newp);
                    AllAction.Add(new ActionNewPrompt(newp, pm));
                }
            }

            Wbs.ReleaseLock();
        }

        public void NewAction_Delete()
        {
            IRange selectedRows = BookView.ActiveView.RangeSelection.Rows;
            Wbs.GetLock();
            foreach (IRange row in selectedRows)
            {
                PromptModel pm = new PromptModel(row.EntireRow);
                AllAction.Add(new ActionDeletePrompt(pm));
            }
            Wbs.ReleaseLock();

        }
        public void NewAction_Edit()
        {
            IRange selectedRows = BookView.ActiveView.RangeSelection.Rows;
            Wbs.GetLock();
            foreach (IRange row in selectedRows)
            {
                var pm = new PromptModel(row.EntireRow);
                using (PromptSetForm pf = new PromptSetForm(pm) { Text = $"请编辑变量的各项数值" })
                {
                    if (pf.ShowDialog() != DialogResult.OK)
                        continue;
                    var newpm = new PromptModel(row.EntireRow);
                    pf.SyncPrompt(newpm);
                    AllAction.Add(new ActionEditPrompt(pm, newpm));
                }
            }
            Wbs.ReleaseLock();
        }

        public void RefreshActionListView()
        {
            MissionView.ResetListViewSize(GetWorkingList().Count);
            MissionView.ActionViews.Refresh();
        }

        public void DeleteAction()
        {
            var view = MissionView.ActionViews;
            var indices = view.SelectedIndices;
            foreach (int index in indices)
            {
                var model = GetWorkingList()[index];
                AllAction.RemoveAll(x => x.Equals(model));
            }
            RefreshActionListView();
        }


    }
}
